<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" size="mini">
          <el-form-item label="单证号" prop="billCode">
            <el-input
              v-model="queryParams.billCode"
              placeholder="请输入单证号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="批次号" prop="manifestId">
            <el-input
              v-model="queryParams.manifestId"
              placeholder="请输入批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:urgentDisposal:add']"
            >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate"
            :disabled="single"
            v-hasPermi="['system:urgentDisposal:edit']"
            >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
            :disabled="single"
            v-hasPermi="['system:urgentDisposal:remove']"
            >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-upload"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:urgentDisposal:edit']"
          >上传附件
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-check" size="mini" :disabled="single" @click="handleDecl"
                    v-hasPermi="['system:urgentDisposal:decl']">提交审核
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="urgentDisposalList" @selection-change="handleSelectionChange"
                @row-dblclick="showDetail" border size="small">
        <el-empty slot="empty" :image="require('@/assets/images/empty.png')" :image-size="260"></el-empty>
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="单证号" prop="billCode" min-width="240" show-overflow-tooltip/>
        <el-table-column label="批次号" prop="manifestId" min-width="140" show-overflow-tooltip/>
        <el-table-column label="进出口" align="center" prop="iEFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_i_e_flag" :value="scope.row.iEFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="进出场类型" align="center" prop="iEType" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_i_e_type" :value="scope.row.iEFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>

        <el-table-column label="异常信息" align="center" prop="errMsg" min-width="200" show-overflow-tooltip/>
        <el-table-column label="申请事由" align="center" prop="disposalNote" min-width="150" show-overflow-tooltip/>
        <el-table-column label="附件状态" align="center" prop="attachStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_attach_file_status" :value="scope.row.attachStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="附件" align="center" prop="attachFile" min-width="120">
          <template slot-scope="scope">
             <div v-if="scope.row.attachFile != ''&&scope.row.attachFile !=null">
              <a :href="baseUrl + scope.row.attachFile" style="text-decoration: underline;" target="_blank">
                <el-button icon="el-icon-view" size="mini" type="primary">查看附件</el-button>
              </a>
            </div>
            <div v-else>
              未上传附件
            </div>
          </template>
        </el-table-column>
        <el-table-column label="舱单重量" align="center" prop="goodsWt"/>
        <el-table-column label="车皮重量" align="center" prop="veWt"/>
        <el-table-column label="集装箱重量" align="center" prop="contaWt" min-width="120" show-overflow-tooltip/>
        <el-table-column label="总过磅称重" align="center" prop="grossWt" min-width="100"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="160" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-upload"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:urgentDisposal:edit']"
            >上传附件
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="showDetail(scope.row)"
              v-hasPermi="['system:urgentDisposal:query']"
            >详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </basic-container>

    <!-- 添加或修改异常处置信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body class="self-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="单证号" prop="billCode">
              <el-input v-model="form.billCode" placeholder="请输入单证号" :disabled="isViewDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次号" prop="manifestId">
              <el-input v-model="form.manifestId" placeholder="请输入批次号" :disabled="isViewDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提单号" prop="billNo">
              <el-input v-model="form.billNo" placeholder="请输入提单号" :disabled="isViewDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进出口" prop="iEFlag">
              <el-select v-model="form.iEFlag" placeholder="请选择进出口类型" style="width: 100%;" :disabled="isViewDisabled">
                <el-option v-for="item in dict.type.sys_i_e_flag" :key="item.value" :value="item.value"
                           :label="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="进出场" prop="iEType">
              <el-select v-model="form.iEType" placeholder="请选择进出场类型" style="width: 100%;" :disabled="isViewDisabled">
                <el-option v-for="item in dict.type.sys_i_e_type" :key="item.value" :value="item.value"
                           :label="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="异常信息" prop="errMsg">
              <el-input type="textarea" :autosize="{ minRows: 2 }" v-model="form.errMsg" placeholder="请输入异常信息" :disabled="isViewDisabled"/>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请事由" prop="disposalNote">
              <el-input v-model="form.disposalNote" :autosize="{ minRows: 2 }" type="textarea" placeholder="请输入申请事由" :disabled="isViewDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件" prop="attachFile">
              <template v-if="dialogType === 'detail'">
                <div v-if="form.attachFile != ''&&form.attachFile !=null">
                  <a :href="baseUrl + form.attachFile" style="text-decoration: underline;" target="_blank">
                    <el-button icon="el-icon-view" size="mini" type="primary">查看附件</el-button>
                  </a>
                  </div>
                <div v-else>
                  未上传附件
                </div>
              </template>
              <fileUpload v-else v-model="form.attachFile" :fileType="fileType" :fileSize="fileSize"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddEditForm" v-if="!isViewDisabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="处置申请附件" :visible.sync="file_open" width="800px" append-to-body>
      <el-form ref="file_form" :model="file_form" :rules="urgrules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="异常信息" prop="errMsg">
              <el-input :disabled="true" type="textarea" :rows="5" v-model="file_form.errMsg"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请事由" prop="disposalNote">
              <el-input v-model="file_form.disposalNote" :rows="5" type="textarea" placeholder="请输入申请事由"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件">
              <fileUpload v-model="file_form.attachFile" :fileSize="fileSize" :fileType="fileType"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUrgentDisposal,
  getUrgentDisposal,
  delUrgentDisposal,
  addUrgentDisposal,
  updateUrgentDisposal,
  exportUrgentDisposal,
  declUrgentDisposalList
} from "@/api/billsdecl/urgentDisposal";

export default {
  name: "UrgentDisposal",
  dicts: ['sys_i_e_flag', 'sys_i_e_type', 'sys_attach_file_status'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_FILE_URL,
      fileSize: 1,
      fileType: ["pdf", "png", "jpg", "jpeg"],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常处置信息表格数据
      urgentDisposalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogType: "add",
      file_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billCode: null,
        manifestId: null,
      },
      // 表单参数
      form: {},
      //文件上传附件
      file_form: {},
      // 表单校验
      urgrules: {
        disposalNote: [{
          required: true,
          message: "申请事由不能为空",
          trigger: "blur"
        }]
      },
      rules: {
        manifestId: [{
          required: true,
          message: "批次号不能为空",
          trigger: "blur"
        }],
        iEFlag: [{
          required: true,
          message: "进出口不能为空",
          trigger: "blur"
        }],
        iEType: [{
          required: true,
          message: "进出场不能为空",
          trigger: "blur"
        }],
        disposalNote: [{
          required: true,
          message: "申请事由不能为空",
          trigger: "blur"
        }],
        attachFile: [{
          required: true,
          message: "附件不能为空",
          trigger: "blur"
        }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询异常处置信息列表 */
    getList() {
      this.loading = true;
      listUrgentDisposal(this.queryParams).then(response => {
        this.urgentDisposalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.file_open = false;
      this.fiel_form = {};
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customCode: null,
        customName: null,
        billCode: null,
        manifestId: null,
        billNo: null,
        entryNo: null,
        iEFlag: null,
        iEType: null,
        disposalObj: null,
        objType: null,
        errMsg: null,
        disposalType: null,
        disposalNote: null,
        disposalStatus: "0",
        disposalResult: null,
        disposalEr: null,
        disposalTime: null,
        createBy: null,
        createTime: null,
        fieldCode: null,
        auditAllClass: null,
        auditCurClass: null,
        attachFile: null,
        waitItemId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.dialogType = 'add';
      this.isViewDisabled = false;
      this.title = "添加异常处置信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.dialogType = 'edit';
      this.title = "修改异常处置信息";
      this.isViewDisabled = false;
      const id = row.id || this.ids;
      getUrgentDisposal(id).then(response => {
        this.form = response.data;
        this.open = true;
        // this.file_form = response.data;
        // this.file_open = true;
      });
    },
    /** 提交审核按钮操作 */
    handleDecl(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认提交审核附件？').then(function () {
        return declUrgentDisposalList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {
      });
    },


    //显示详情
    showDetail(row, column, event) {
      this.reset();
      //关闭表单编辑模式
      this.isViewDisabled = true;
      const id = row.id;
      getUrgentDisposal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.dialogType = 'detail';
        this.title = "详情";
      });
    },
    // 新增、修改
    submitAddEditForm() {
       this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.dialogType === 'add') {
            addUrgentDisposal(this.form).then(res => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            })
          } else {
             updateUrgentDisposal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
       })
    },
    /** 提交处置按钮 */
    submitForm() {
      this.$refs["file_form"].validate(valid => {
        if (valid) {
          if (this.file_form.id != null) {
            updateUrgentDisposal(this.file_form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.file_open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除异常处置信息编号为"' + ids + '"的数据项？').then(function () {
        return delUrgentDisposal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有异常处置信息数据项？').then(() => {
        this.exportLoading = true;
        return exportUrgentDisposal(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/ .el-input.is-disabled .el-input__inner {
  color: #606266;
}
/deep/ .el-textarea.is-disabled .el-textarea__inner {
  color: #606266;
}
</style>
